﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Delay;
using System.ComponentModel;

namespace PhoneInfo.Page
{
    public partial class RegisterAccount : PhoneApplicationPage
    {
        private RegisteAccount registeAccount;
        public RegisterAccount()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (sender==null)
            {
                return;
            }
           string user=userName.Text;
           if (user.Length==0)
            {
                MessageBox.Show("用户名不能为空");
                return;
            }
           string psw = passWord.Password;
           registeAccount = new RegisteAccount();
           registeAccount.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(registeAccount_PropertyChanged);
           registeAccount.SetValue(LowProfileXmlLoader.UriSourceProperty, new Uri(registeAccount.GetPhoneInfoUrl(user, psw)));
           PopWait.IsOpen = true;
           
        }
        void registeAccount_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (sender==null)
            {
                return;
            }
            RegisteAccount ra = sender as RegisteAccount;
            if (ra==null)
            {
                return;
            }
            if (e.PropertyName=="Result")
            {
                switch (ra.Result)
                {
                    case -1:
                    
                    case 1:
                        ToastPrompt tmp = new ToastPrompt()
                        {
                            Message = ra.ResultMsg
                        };
                        tmp.Show();
                        PopWait.IsOpen = false;
                        //失败
                      break;
                    case 2:
                        //写入数据库
                      userTable table = new userTable()
                      {
                          Nick = ra.Nick,
                          Headimg = ra.Headimg,
                          Type = "Head",
                          UserName = ra.UserName,
                          PassWord = ra.PassWord,
                          Userid = ra.Userid,
                          Lxt = ra.Lxt,
                          Sex = ra.Sex,
                          Sign = ra.Sign,
                          Viprank = ra.Viprank,
                          Vipimgurl = ra.Vipimgurl,
                          Userrank = ra.Userrank,
                          Isreg = ra.Isreg
                      };
                       App.lxt=ra.Lxt;
                       writeDB(loadDB(), table);
                       PopWait.IsOpen = false;
                       NavigationService.GoBack();
                      break;
                    

                }
                
            }
            //
        }
        private List<userTable> loadDB()
        {
            var userInDB = (from userTable user in App.ManagerDB.accountTable
                            select user).ToList();
            return userInDB;
        }
        private void writeDB(List<userTable> list, userTable table)
        {
            bool isExit = false;
            foreach (userTable item in list)
            {
                if (item.UserName == table.UserName)
                {
                    item.Type = "Head";
                    isExit = true;
                }
            }
            if (isExit == false)
            {//数据库中不存在该用户名
                App.ManagerDB.accountTable.InsertOnSubmit(table);
                App.ManagerDB.SubmitChanges();
            }
            foreach (userTable item in list)
            {
                if ( item.Type == "Head")
                {
                    if(  item.UserName != table.UserName)                 
                      item.Type = "Main";
                    
                }
            }
        }

    }
}